﻿shogunate_historical_friend_effect = {

	if = {
		limit = {
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR1$ = {
			set_relation_friend = character:$CHAR2$
		}
	}

}

shogunate_historical_rival_effect = {

	if = {
		limit = {
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR1$ = {
			set_relation_rival = character:$CHAR2$
		}
	}

}

shogunate_historical_alliance_effect = {

	if = {
		limit = {
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR1$ = {
			create_alliance = {
				target = character:$CHAR2$
				allied_through_owner  = character:$CHAR1$
				allied_through_target = character:$CHAR2$
			}
		}
		character:$CHAR2$ = {
			add_opinion = {
				modifier = perk_negotiated_alliance_opinion
				target = character:$CHAR1$
			}
		}
	}

}

shogunate_historical_break_alliance_effect = {

	if = {
		limit = {
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR1$ = {
			break_alliance = character:$CHAR2$
		}
	}

}

shogunate_historical_ambition_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
				is_ai = yes
			}
			character:$CHAR$ = {
				NOT = {
					any_vassal_or_below = {
						has_title = title:$TITLE$
					}
				}
			}
		}
		character:$CHAR$ = {
			add_pressed_claim = title:$TITLE$
		}
	}

}

shogunate_historical_heir_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR1$ = {
			set_designated_heir = character:$CHAR2$
		}
	}

}

shogunate_historical_trait_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = { is_alive = yes }
		}
		character:$CHAR$ = {
			add_trait = $TRAIT$
		}
	}

}

shogunate_historical_remove_trait_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = { is_alive = yes }
		}
		character:$CHAR$ = {
			remove_trait = $TRAIT$
		}
	}

}

shogunate_historical_perk_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = { is_alive = yes }
		}
		character:$CHAR$ = {
			add_perk = $PERK$
		}
	}

}

shogunate_historical_surprise_attack_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = { is_alive = yes }
		}
		character:$CHAR$ = {
			add_character_modifier = {
				modifier = shogunate_surprise_attack_modifier
				months = $MONTH$
			}
		}
	}

}

shogunate_historical_reinforcement_effect = {

	if = {
		limit = {
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR1$ = {
			add_opinion = {
				modifier = shogunate_historical_reinforcement_opinion
				months = $MONTH$
				target = character:$CHAR2$
			}
		}
	}

}

shogunate_historical_vassal_contract_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = { is_alive = yes }
		}
		character:$CHAR$ = {
			vassal_contract_set_obligation_level = { type = feudal_government_taxes  level = $LEVEL$ }
			vassal_contract_set_obligation_level = { type = feudal_government_levies level = $LEVEL$ }
		}
	}

}

shogunate_historical_realm_law_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = { is_alive = yes }
		}
		character:$CHAR$ = {
			add_realm_law_skip_effects = $LAW$
		}
	}

}

shogunate_historical_culture_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = { is_alive = yes }
		}
		character:$CHAR$ = {
			set_culture = culture:$CULTURE$
			every_child = {
				set_culture = culture:$CULTURE$
			}
		}
	}

}

shogunate_historical_capital_effect = {

	if = {
		limit = {
			exists = title:$TITLE$
			exists = title:$TITLE$.holder
		}
		title:$TITLE$.holder = {
			set_realm_capital = title:$TITLE$
		}
	}

}

shogunate_historical_duchy_capital_effect = {

	if = {
		limit = {
			exists = title:$TITLE$
			exists = title:$TITLE$.holder
		}
		title:$TITLE$.de_jure_liege = {
			set_capital_county = title:$TITLE$
		}
		title:$TITLE$.holder = {
			set_realm_capital = title:$TITLE$
		}
	}

}

shogunate_historical_kingdom_capital_effect = {

	if = {
		limit = {
			exists = title:$TITLE$
			exists = title:$TITLE$.holder
		}
		title:$TITLE$.de_jure_liege.de_jure_liege = {
			set_capital_county = title:$TITLE$
		}
		title:$TITLE$.holder = {
			set_realm_capital = title:$TITLE$
		}
	}

}

shogunate_historical_empire_capital_effect = {

	if = {
		limit = {
			exists = title:$TITLE$
			exists = title:$TITLE$.holder
		}
		title:$TITLE$.de_jure_liege.de_jure_liege.de_jure_liege = {
			set_capital_county = title:$TITLE$
		}
		title:$TITLE$.holder = {
			set_realm_capital = title:$TITLE$
		}
	}

}

shogunate_ai_personality_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
				is_ai = yes
			}
		}
		character:$CHAR$ = {
			add_character_modifier = {
				modifier = shogunate_personality_$CHAR$_modifier
				years = $YEARS$
			}
		}
	}

}

shogunate_ai_trait_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
				is_ai = yes
			}
		}
		character:$CHAR$ = {
			add_trait = $TRAIT$
		}
	}

}

shogunate_ai_remove_trait_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
				is_ai = yes
			}
		}
		character:$CHAR$ = {
			remove_trait = $TRAIT$
		}
	}

}

shogunate_ai_will_not_convert_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
			}
		}
		character:$CHAR$ = {
			add_character_flag = ai_will_not_convert
		}
	}

}

shogunate_historical_regency_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR1$ = {
			start_diarchy = regency
			set_diarch = character:$CHAR2$
			set_diarchy_swing = $POWER$
		}
	}

}

shogunate_historical_co_emperor_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = {
				is_alive = yes
				exists = primary_title
				primary_title.tier >= tier_empire
			}
			character:$CHAR2$ = {
				is_alive = yes
				top_liege ?= character:$CHAR1$
			}
		}
		character:$CHAR1$ = {
			start_diarchy = co_emperorship
			set_diarch = character:$CHAR2$
			if = {
				limit = {
					character:$CHAR2$ = {
						is_close_or_extended_family_of = character:$CHAR1$
						house ?= character:$CHAR1$.house
					}
				}
				set_designated_heir = character:$CHAR2$
			}
		}
		character:$CHAR2$ = {
			set_variable = {
				name = co_emp_appointer
				value = character:$CHAR1$
			}
			add_opinion = {
				target = character:$CHAR1$
				modifier = made_co_ruler_opinion
				opinion = 50
			}
		}
	}

}

shogunate_historical_hostage_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR2$ = {
			take_hostage = character:$CHAR1$
		}
	}

}

shogunate_historical_imprison_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = { is_alive = yes }
			character:$CHAR2$ = { is_alive = yes }
		}
		character:$CHAR2$ = {
			imprison = {
				target = character:$CHAR1$
				type = $TYPE$
			}
		}
	}

}

shogunate_historical_feud_effect = {

	if = {
		limit = {
			exists = character:$CHAR1$
			exists = character:$CHAR2$
			character:$CHAR1$ = {
				is_alive = yes
				exists = house
				exists = house.house_head
				this = house.house_head
			}
			character:$CHAR2$ = {
				is_alive = yes
				exists = house
				exists = house.house_head
				this = house.house_head
			}
		}
		character:$CHAR1$ = { save_scope_as = actor }
		character:$CHAR2$ = { save_scope_as = target }
		scope:actor = {
			trigger_event = shogunate_bookmark.8310
		}
	}

}

shogunate_historical_house_unity_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
				exists = house
				exists = house.house_head
				this = house.house_head
			}
		}
		character:$CHAR$.house = {
			add_unity_value = {
				value = $DIFF$
				character = character:$CHAR$
				desc = clan_unity_historical_circumstances.desc
			}
		}
	}

}

shogunate_historical_offmap_character_effect = {

	if = {
		limit = {
			OR = {
				game_start_date <  $FROM$
				game_start_date >= $TO$
			}
			character:$CHAR$ ?= {
				is_alive = yes
			}
		}
		character:$CHAR$ = {
			death = { death_reason = death_vanished }
		}
	}

}

shogunate_historical_landless_adventurer_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
				is_adult = yes
				is_ruler = no
			}
		}
		character:$CHAR$ = {
			if = {
				limit = {
					is_courtier = yes
					exists = liege
				}
				set_variable = {
					name = shogunate_landless_adventurer_historical_liege
					value = liege
				}
				liege = {
					remove_courtier_or_guest = character:$CHAR$
				}
			}
			else_if = {
				limit = {
					any_province = {
						this.culture = character:$CHAR$.culture
					}
				}
				random_province = {
					limit = {
						this.culture = character:$CHAR$.culture
					}
					save_scope_as = starting_location
				}
				set_location = scope:starting_location
			}
			else = {
				title:e_nmih_japan ?= {
					random_de_jure_county = {
						title_province = {
							save_scope_as = starting_location
						}
					}
				}
				set_location = scope:starting_location
			}
			create_landless_adventurer_title_effect = {
				REASON = flag:historical
				FLAVOR_CHAR = character:$CHAR$
			}
			if = {
				limit = {
					is_female = yes
				}
				add_realm_law_skip_effects = male_preference_law
			}
			add_realm_law_skip_effects = camp_purpose_$PURPOSE$
			remove_variable = flavourise_camp_purpose_wanderers
			set_variable = flavourise_camp_purpose_$PURPOSE$
		}
	}

}

shogunate_historical_landless_adventurer_with_location_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
				is_adult = yes
				is_ruler = no
			}
		}
		character:$CHAR$ = {
			if = {
				limit = {
					is_courtier = yes
					exists = liege
				}
				set_variable = {
					name = shogunate_landless_adventurer_historical_liege
					value = liege
				}
				liege = {
					remove_courtier_or_guest = character:$CHAR$
				}
			}
			title:$LOCATION$ = {
				random_de_jure_county = {
					title_province = {
						save_scope_as = starting_location
					}
				}
			}
			set_location = scope:starting_location
			create_landless_adventurer_title_effect = {
				REASON = flag:historical
				FLAVOR_CHAR = character:$CHAR$
			}
			add_realm_law_skip_effects = camp_purpose_$PURPOSE$
			remove_variable = flavourise_camp_purpose_wanderers
			set_variable = flavourise_camp_purpose_$PURPOSE$
		}
	}

}

shogunate_historical_landless_follower_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_landless_adventurer = yes
			}
			exists = character:$FOLLOWER$
			character:$FOLLOWER$ = {
				is_alive = yes
				is_ruler = no
			}
		}
		character:$FOLLOWER$ = {
			if = {
				limit = {
					is_courtier = yes
					exists = liege
				}
				set_variable = {
					name = shogunate_landless_adventurer_historical_liege
					value = liege
				}
				liege = {
					remove_courtier_or_guest = character:$FOLLOWER$
				}
			}
			character:$CHAR$ = {
				add_courtier = character:$FOLLOWER$
			}
			return_to_court = yes
		}
	}

}

shogunate_historical_landless_return_to_court_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_landless_adventurer = yes
			}
			NOT = {
				any_player = {
					this = character:$CHAR$
				}
			}
		}
		character:$CHAR$ = {
			every_courtier = {
				limit = {
					has_variable = shogunate_landless_adventurer_historical_liege
				}
				save_scope_as = landless_follower
				var:shogunate_landless_adventurer_historical_liege = {
					add_courtier = scope:landless_follower
				}
				return_to_court = yes
				remove_variable = shogunate_landless_adventurer_historical_liege
			}
			destroy_title = character:$CHAR$.primary_title
			if = {
				limit = {
					has_variable = shogunate_landless_adventurer_historical_liege
				}
				save_scope_as = landless_adventurer
				var:shogunate_landless_adventurer_historical_liege = {
					add_courtier = scope:landless_adventurer
				}
				return_to_court = yes
				remove_variable = shogunate_landless_adventurer_historical_liege
			}
			remove_variable = flavourise_camp_purpose_wanderers
			remove_variable = flavourise_camp_purpose_mercenaries
			remove_variable = flavourise_camp_purpose_scholars
			remove_variable = flavourise_camp_purpose_explorers
			remove_variable = flavourise_camp_purpose_brigands
			remove_variable = flavourise_camp_purpose_legitimists
		}
	}

}

shogunate_historical_landless_naming_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_landless_adventurer = yes
			}
		}
		character:$CHAR$.primary_title = {
			set_title_name = $NAME$
		}
	}

}

shogunate_historical_landless_naming_with_date_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_landless_adventurer = yes
			}
		}
		character:$CHAR$.primary_title = {
			set_title_name = $NAME$
		}
	}

}

shogunate_historical_landless_building_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
			character:$CHAR$ = {
				is_alive = yes
				is_landless_adventurer = yes
			}
		}
		every_domicile = {
			limit = {
				owner ?= character:$CHAR$
				is_domicile_type = camp
			}
			add_domicile_building = $BUILDING$
		}
	}

}

shogunate_historical_government_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = title:$TITLE$
			exists = title:$TITLE$.holder
			title:$TITLE$ = title:$TITLE$.holder.primary_title
			title:$TITLE$.holder = {
				is_ai = yes
				is_alive = yes
			}
		}
		title:$TITLE$.holder = {
			change_government = $GOVERNMENT$
			add_realm_law_skip_effects = $SUCCESSION$

			if = {
				limit = {
					shogunate_nanbokucho_period_start_date_trigger = yes
					NOT = { government_has_flag = government_is_tribal }
				}
				add_realm_law_skip_effects = male_only_law
			}
			else = {
				add_realm_law_skip_effects = male_preference_law
			}
		}
	}

}

shogunate_historical_theocracy_government_effect = {

	shogunate_historical_government_effect = {
		TITLE      = $TITLE$
		FROM       = $FROM$
		TO         = $TO$
		GOVERNMENT = theocracy_government
		SUCCESSION = bishop_theocratic_succession_law
	}

}

shogunate_historical_republic_government_effect = {

	shogunate_historical_government_effect = {
		TITLE      = $TITLE$
		FROM       = $FROM$
		TO         = $TO$
		GOVERNMENT = republic_government
		SUCCESSION = city_succession_law
	}

}

shogunate_historical_administrative_government_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = title:$TITLE$
			exists = title:$TITLE$.holder
			title:$TITLE$ = title:$TITLE$.holder.primary_title
		}
		title:$TITLE$.holder = {
			convert_to_administrative_from_feudalism_game_start_effect = yes
		}
	}

}

shogunate_historical_special_building_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = title:$TITLE$
		}
		title:$TITLE$.title_province ?= {
			add_special_building = $TYPE$

			### Copied from on_building_completed in /common/on_action/province_on_actions.txt

			if = {
				limit = {
					has_special_building = no
				}
			}
			else_if = {
				limit = {
					OR = {
						has_building_with_flag = { flag = travel_point_of_interest_martial }
						has_building_with_flag = { flag = travel_point_of_interest_martial_minor }
					}
					NOT = { has_travel_point_of_interest = poi_special_buildings_martial }
				}
				add_travel_point_of_interest = poi_special_buildings_martial
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_learning }
					NOT = { has_travel_point_of_interest = poi_special_buildings_learning }
				}
				add_travel_point_of_interest = poi_special_buildings_learning
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_religious }
					NOT = { has_travel_point_of_interest = poi_special_buildings_religious }
				}
				add_travel_point_of_interest = poi_special_buildings_religious
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_diplomatic }
					NOT = { has_travel_point_of_interest = poi_special_buildings_diplomatic }
				}
				add_travel_point_of_interest = poi_special_buildings_diplomatic
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_wonder }
					NOT = { has_travel_point_of_interest = poi_special_buildings_wonder }
				}
				add_travel_point_of_interest = poi_special_buildings_wonder
			}
			else_if = {
				limit = {
					has_building_with_flag = { flag = travel_point_of_interest_economic }
					NOT = { has_travel_point_of_interest = poi_special_buildings_economic }
				}
				add_travel_point_of_interest = poi_special_buildings_economic
			}
		}
	}

}

shogunate_historical_special_building_slot_effect = {

	if = {
		limit = {
			game_start_date >= $FROM$
			game_start_date <  $TO$
			exists = title:$TITLE$
		}
		title:$TITLE$.title_province ?= {
			add_special_building_slot = $TYPE$
		}
	}

}

shogunate_randomize_name_effect = {

	if = {
		limit = {
			exists = character:$CHAR$
		}
		character:$CHAR$ = {
			save_scope_as = original
			random_province = {
				save_scope_as = birthplace
			}
			create_character = {
				gender = scope:original
				faith = scope:original.faith
				culture = scope:original.culture
				location = scope:birthplace
				save_scope_as = template
			}
			set_house = scope:template.house
			change_first_name = { template_character = scope:template }
			scope:template = {
				death = { death_reason = death_vanished }
			}
		}
	}

}
